package simple

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */

type ListNode struct {
	Val  int
	Next *ListNode
}

// https://leetcode-cn.com/problems/reverse-linked-list/
func reverseList(head *ListNode) *ListNode {
	if nil == head {
		return nil
	}

	// 遍历将整个链表放到数组中
	listSlice := make([]*ListNode, 0)
	tempHead := head
	for nil != tempHead {
		listSlice = append(listSlice, tempHead)
		tempHead = tempHead.Next
	}

	newHead := new(ListNode)
	newHead.Val = listSlice[len(listSlice)-1].Val
	currentHead := newHead
	for index := len(listSlice) - 2; index >= 0; index-- {
		currentHead.Next = new(ListNode)
		currentHead = currentHead.Next
		currentHead.Val = listSlice[index].Val
	}

	return newHead
}
